package com.jsbj.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.jsbj.base.ResultBean;
import com.jsbj.base.WXConstant;
import com.jsbj.framework.controller.BaseController;
import com.jsbj.page.PageReq;
import com.jsbj.tools.StringUtils;
import com.jsbj.tools.WechatUtil;
import com.jsbj.user.entities.SysOrg;
import com.jsbj.user.entities.SysUser;
import com.jsbj.user.service.ISysOrgService;
import com.jsbj.user.service.ISysUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 * 系统登录用户表 前端控制器
 * </p>
 *
 * @author jsbj
 * @since 2022-02-21
 */
@RestController
@RequestMapping("/sys-user")
@Api(value = "用户管理", tags = "用户管理")
@ApiIgnore
@Slf4j
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysOrgService sysOrgService;

    @ApiIgnore
    @ApiOperation("注册接口")
    @ApiImplicitParam(name = "inputParam", value = "注册接口", required = true)
    @PostMapping("/api/registerUser")
    public ResultBean<Object> registerUser(@RequestBody String inputParam) {
        JSONObject object = JSONObject.parseObject(inputParam);
        return sysUserService.registerUser(object);
    }

    @ApiIgnore
    @ApiOperation("注册/修改密码发送验证码短信")
    @ApiImplicitParam(paramType = "query", name = "phoneNumber", value = "手机号", required = true)
    @PostMapping("/api/sendMessage")
    public ResultBean sendMessage(@RequestParam String phoneNumber) {
        ResultBean ajax = sysUserService.sendMess(phoneNumber,null);
        return ajax;
    }

    @ApiIgnore
    @ApiOperation("发送验证码短信(品质工程)")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "phoneNumber", value = "手机号", required = true),
            @ApiImplicitParam(paramType = "query", name = "signName", value = "短信签名", required = false)})
    @PostMapping("/api/sendMessageBySign")
    public ResultBean sendMessageBySign(@RequestParam String phoneNumber, @RequestParam String sign) {
        ResultBean ajax = sysUserService.sendMess(phoneNumber,sign);
        return ajax;
    }

    @ApiIgnore
    @ApiOperation("验证注册/修改密码发送的验证码")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "phoneNumber", value = "手机号", required = true),
            @ApiImplicitParam(paramType = "query", name = "message", value = "验证码", required = true)})
    @PostMapping("/api/checkMessage")
    public ResultBean checkMessage(@RequestParam String phoneNumber, @RequestParam String message) {
        ResultBean ajax = sysUserService.checkMessage(phoneNumber, message);
        return ajax;
    }

    @ApiOperation("修改密码接口")
    @ApiImplicitParam(name = "inputParam", value = "修改密码接口", required = true)
    @PostMapping("/api/updatePassword")
    public ResultBean<Object> updatePassword(@RequestBody String inputParam) {
        JSONObject object = JSONObject.parseObject(inputParam);
        return sysUserService.updatePassword(object);
    }

    @ApiIgnore
    @ApiOperation("根据租户编码查询有权限登录的人员信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "tenantCode", value = "租户编码", required = true)})
    @PostMapping("/getUsersByTenantCode")
    public List<Map<String, Object>> getUsersByTenantCode(@RequestParam String tenantCode) {
        return sysUserService.getUsersByTenantCode(tenantCode);
    }

    @ApiOperation("根据租户查询人员列表")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "tenantCode", value = "租户编码", required = true)})
    @PostMapping("/queryUsersByTenantCode")
    public ResultBean<List<SysUser>> queryUsersByTenantCode(@RequestParam String tenantCode) {
        return new ResultBean<>(sysUserService.queryUsersByTenantCode(tenantCode));
    }

    @ApiOperation("根据用户id查询人员信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", required = true)})
    @GetMapping("/api/getUserById")
    public ResultBean<SysUser> getUserById(@RequestParam String userId) {
        return new ResultBean<>(sysUserService.getById(userId));
    }

    @ApiIgnore
    @ApiOperation("根据租户编码查询超管账号和密码")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "tenantCode", value = "租户编码", required = true)})
    @GetMapping("/api/getUserByTenantCode")
    public ResultBean<SysUser> getUserByTenantCode(@RequestParam String tenantCode) {
        return new ResultBean<>(sysUserService.getUserByTenantCode(tenantCode));
    }

    @ApiIgnore
    @ApiOperation("根据租户编码查询所有登录账号")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "tenantCode", value = "租户编码", required = true)})
    @GetMapping("/getAllUserByTenantCode")
    public ResultBean<List<String>> getAllUserByTenantCode(@RequestParam String tenantCode) {
        return new ResultBean<List<String>>(sysUserService.getAllUserByTenantCode(tenantCode));
    }

    @ApiOperation(value = "添加人员")
    @PostMapping("/add")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "tenantCode", value = "租户id", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "Integer", name = "userId", value = "用户登录id", required = true)}
    )
    public ResultBean<String> add(@RequestBody SysUser sysUser) throws Exception {
        return sysUserService.add(sysUser);
    }

    @ApiOperation(value = "添加人员")
    @PostMapping("/api/feignAdd")
    public Integer feignAdd(@RequestBody SysUser sysUser) throws Exception {
        return sysUserService.feignAdd(sysUser);
    }

    @ApiOperation(value = "删除人员(供内部调用)")
    @PostMapping("/api/feignDelete")
    public Integer feignDelete(@RequestParam String id,@RequestParam String userId) {
        return sysUserService.feignDelete(id,userId);
    }

    @ApiOperation(value = "删除")
    @ResponseBody
    @PostMapping("/delete")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "tenantCode", value = "租户id", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "Integer", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "数据id", required = true)}
    )
    public ResultBean delete(@RequestParam String id) {
        return new ResultBean<Integer>(sysUserService.delete(id));
    }

    @ApiOperation(value = "查询列表")
    @ResponseBody
    @PostMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "tenantCode", value = "租户id", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "Integer", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "页码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页条数", required = true)}
    )
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysUser.class)})
    public ResultBean<PageReq> getList(@RequestBody String inParam) {
        return new ResultBean<PageReq>(getPage(sysUserService.getList(inParam)));
    }

    @ApiOperation("查询组织树")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @ApiResponses({@ApiResponse(code = 200, message = "请求成功", response = SysOrg.class)})
    @PostMapping("/getOrgTreeByQx")
    public ResultBean<List<SysOrg>> getOrgTreeByQx(@RequestBody String inParam) {
        return new ResultBean<List<SysOrg>>(sysOrgService.getOrgTreeByQx(inParam));
    }

    @ApiOperation(value = "密码重置")
    @ResponseBody
    @PostMapping("/passwordReset")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "tenantCode", value = "租户id", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "Integer", name = "userId", value = "用户登录id", required = true)}
    )
    public ResultBean<Integer> passwordReset(@RequestBody String inParam) {
        return new ResultBean<Integer>(sysUserService.passwordReset(inParam));
    }

    @ApiOperation("模板下载")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "tenantCode", value = "租户id", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orgCode", value = "标段编码", required = true)}
    )
    @GetMapping(value = "/downloadTemplate")
    public ResultBean<Integer> downloadTemplate(HttpServletResponse response , @RequestParam String tenantCode) {
//        if (StringUtils.isEmpty(orgCode)) {
//            return new ResultBean<Integer>().build("标段不能为空！");
//        }
        sysUserService.downloadTemplate(response ,tenantCode);
        return new ResultBean<Integer>(1);
    }

    @ApiOperation(value = "导入模板", notes = "导入模板")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "tenantCode", value = "租户id", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    @PostMapping(value = "/importExcel")
    public ResultBean<Map<String, Object>> importExcel(@RequestParam("uploadFile") MultipartFile uploadFile, @RequestParam String tenantCode) {

        return new ResultBean(sysUserService.importExcel(uploadFile, tenantCode));
    }

    /**
     * 通过登陆id查询登陆人
     *
     * @param loginId
     * @return
     */
    @PostMapping("/queryByLoginId")
    public ResultBean<SysUser> queryByLoginId(String loginId){
        return new ResultBean<>(sysUserService.queryByLoginId(loginId));
    }

    @PostMapping("/queryByLoginIdFeign")
    public SysUser queryByLoginIdFeign(@RequestParam String loginId){
        return sysUserService.queryByLoginId(loginId);
    }



    @GetMapping("/superUserFlag")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "tenantCode", value = "租户id", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    public ResultBean<Boolean> superUserFlag() {
        return new ResultBean<>(sysUserService.getSuperUserFlag());
    }

    @GetMapping("/getUser")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "tenantCode", value = "租户id", required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "userId", value = "用户登录id", required = true)}
    )
    public ResultBean<List<Map<String, Object>>> getUser() {
        return new ResultBean<>(sysUserService.getUser());
    }

    @ApiOperation("根据用户id查询人员信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", required = true)})
    @GetMapping("/detail")
    public ResultBean<SysUser> detail(@RequestParam String userId) {
        return new ResultBean<>(sysUserService.getDetail(userId));
    }

    @ApiIgnore
    @PostMapping("/getUsersByTenantCodeAndOrgCode")
    public List<Map<String, Object>> getUsersByTenantCodeAndOrgCode(@RequestParam String tenantCode, @RequestParam String orgCode) {
        return sysUserService.getUsersByTenantCodeAndOrgCode(tenantCode, orgCode);
    }

    @ApiIgnore
    @PostMapping("/api/getUsersByTenantCodeAndOrgCodeApi")
    public List<Map<String, Object>> getUsersByTenantCodeAndOrgCodeApi(@RequestParam String tenantCode, @RequestParam String orgCode) {
        return sysUserService.getUsersByTenantCodeAndOrgCode(tenantCode, orgCode);
    }

    @ApiIgnore
    @PostMapping("/configExpertQX")
    public void configExpertQX(@RequestParam List<String> loginIds, @RequestParam List<String> delLoginIds, @RequestParam String tenantCode) {
        sysUserService.configExpertQX(loginIds, delLoginIds, tenantCode);
    }

    @PostMapping("/api/getByLoginIds")
    public List<String> getByLoginIds(@RequestParam List<String> loginIds) {
        return sysUserService.getByLoginIds(loginIds);
    }
}

